#include <iostream>

#include "classifier.h"
#include "common.h"
#include <glog/logging.h>
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>

#define MASK_GEN_TEST    1
#define MLDB_TEST        0
#define LOAD_IMAGE       0


using google::INFO;
using boost::filesystem::path;

void DataBaseTest();

void MyTraining();

int main() {
    backward::SignalHandling sh;
    if (!sh.loaded()) {
        LOG(ERROR) << "BACKWARD NOT LOADED";
    }

    LOG(INFO) << "hello";
    DataBaseTest();
    MyTraining();
    return 0;
}


/*
 * This function tries to use customer training method to train
 * the MNIST data set using original Net.
 */
void MyTraining() {

    exit(0);
}


void DataBaseTest() {
#if MASK_GEN_TEST
    DRImage drImage(cv::imread("/home/d/workspace/DeepDR/data/imgs_hospital/C0011141.jpg"));
    auto mask = drImage.getMask();
    mask.show();
#endif
    Common::printTrace();

#if MLDB_TEST
    LMDBDataSet lmdbDataSet;
    lmdbDataSet.loadFromLMDB("/home/d/caffe/examples/mnist/mnist_test_lmdb");
    LOG(INFO) << "size of the database is " << lmdbDataSet.length();
    lmdbDataSet.seek(100);
    caffe::Datum datum;
    lmdbDataSet.getDatum(datum);
    LOG(INFO) << "Get Channels : " << datum.channels() <<
    " Size" << datum.width() <<
    " x " << datum.height() <<
    " Label " << datum.label();
    Mat retvalImg = lmdbDataSet.getCvMat();
    LOG(INFO) << "Get Channels : " << retvalImg.channels() <<
    " Size: " << retvalImg.cols <<
    " x " << retvalImg.rows;
//    cv::imshow("retrived image", retvalImg);
//    cv::waitKey(0);
    Sample sample = lmdbDataSet.getSample();
//    cv::imshow("sample", sample.data);
//    cv::waitKey();
    LOG(INFO) << "Getting Label " << sample.label;

    LMDBDataSet newDataset;
    path dbpath("testDB");
    if (boost::filesystem::exists(dbpath)) {
        LOG(INFO) << "deleting existing database";
        boost::filesystem::remove_all(dbpath);
    }
    newDataset.newDB(dbpath.string());
    newDataset.addSample(sample);
    if (boost::filesystem::exists(dbpath)) {
        LOG(INFO) << "deleting existing database";
        boost::filesystem::remove_all(dbpath);
    }

#endif

#if LOAD_IMAGE
    Classifier classssifier("/home/d/workspace/DeepDR/data/imgs_hospital/trainfiles.csv");
    LOG(INFO) << "There are " << classssifier.dg.length() << "samples";
    int hist[10];
    for (int i = 0; i < sizeof(hist) / sizeof(int); i++) {
        hist[i] = 0;
    }
    for (int i = 0; i < classssifier.dg.length(); i++) {
        hist[classssifier.dg[i].label] += 1;
    }
    for (int i = 0; i < sizeof(hist) / sizeof(int); i++) {
        LOG(INFO) << "Class " << i << " has " << hist[i] << " samples";
    }
#endif
    return;
}

